package com.ruoyi.web.controller.gwc;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gwc.domain.TRepairManagement;
import com.ruoyi.gwc.service.ITRepairManagementService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gwc.domain.TApprovalRepair;
import com.ruoyi.gwc.service.ITApprovalRepairService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请维修审批Controller
 * 
 * @author wp
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/gwc/repair")
public class TApprovalRepairController extends BaseController
{
    @Autowired
    private ITApprovalRepairService tApprovalRepairService;

    @Autowired
    private ITRepairManagementService tRepairManagementService;

    /**
     * 查询申请维修审批列表
     */
    @ApiOperation("查询申请维修审批列表")
    @GetMapping("/list")
    public TableDataInfo list(TApprovalRepair tApprovalRepair)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((tApprovalRepair.getCompanyId()))) {
            tApprovalRepair.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<TApprovalRepair> list = tApprovalRepairService.selectTApprovalRepairList(tApprovalRepair);
        return getDataTable(list);
    }

    /**
     * 导出申请维修审批列表
     */
    @Log(title = "申请维修审批", businessType = BusinessType.EXPORT)
    @ApiOperation("导出申请维修审批列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TApprovalRepair tApprovalRepair)
    {
        LoginUser loginUser = getLoginUser();
        TApprovalRepair approvalRepair  = new TApprovalRepair();
        List<TRepairManagement> list = new ArrayList<>();
        Long  approvealStatus = tApprovalRepair.getApprovealStatus();
        //审批状态（0:通过，1：不通过，2：待审批）
        if(approvealStatus == 0 || approvealStatus == 1 ) {
            approvalRepair.setApprovealStatus(approvealStatus);
            if (!loginUser.getUser().isAdmin()) {
                approvalRepair.setCreateBy(loginUser.getUser().getUserName());
            }
            list = tRepairManagementService.getApprovalRepairList(approvalRepair);
            for (TRepairManagement repairManagement : list) {
                if (ObjectUtils.isNotEmpty(repairManagement.getRepairCost())) {
                    repairManagement.setRepairCostStr(repairManagement.getRepairCost().toString());
                }
                if (ObjectUtils.isNotEmpty(repairManagement.getExpectCost())) {
                    repairManagement.setExpectCostStr(repairManagement.getExpectCost().toString());
                }
                if (ObjectUtils.isNotEmpty(repairManagement.getRepairRepair())) {
                    repairManagement.setRepairRepairStr(repairManagement.getRepairRepair().toString());
                }
            }
        }else {
            TRepairManagement tRepairManagement = new TRepairManagement();
            if (!loginUser.getUser().isAdmin()) {
                tRepairManagement.setCompanyId(loginUser.getUser().getDeptId());
            }
            tRepairManagement.setOrderStatus(0L);//订单状态（0:已申请，1：已审批，2：已送修，3：已完成，4：已撤销 5：已驳回）
            list = tRepairManagementService.selectTRepairManagementList(tRepairManagement);
            for (TRepairManagement repairManagement : list ){
                if(ObjectUtils.isNotEmpty(repairManagement.getRepairCost())) {
                    repairManagement.setRepairCostStr(repairManagement.getRepairCost().toString());
                }
                if(ObjectUtils.isNotEmpty(repairManagement.getExpectCost())){
                    repairManagement.setExpectCostStr(repairManagement.getExpectCost().toString());
                }
                if(ObjectUtils.isNotEmpty(repairManagement.getRepairRepair())){
                    repairManagement.setRepairRepairStr(repairManagement.getRepairRepair().toString());
                }
            }
        }

        ExcelUtil<TRepairManagement> util = new ExcelUtil<TRepairManagement>(TRepairManagement.class);
        util.hideColumn("remark");
        util.exportExcel(response, list, "申请维修审批数据");
    }

    /**
     * 获取申请维修审批详细信息
     */
    @ApiOperation("获取申请维修审批详细信息")
    @GetMapping(value = "/{approvalId}")
    public AjaxResult getInfo(@PathVariable("approvalId") Long approvalId)
    {
        return success(tApprovalRepairService.selectTApprovalRepairByApprovalId(approvalId));
    }

    /**
     * 新增申请维修审批
     */
    @Log(title = "申请维修审批", businessType = BusinessType.INSERT)
    @ApiOperation("新增申请维修审批")
    @PostMapping
    public AjaxResult add(@RequestBody TApprovalRepair tApprovalRepair)
    {
        if (tApprovalRepairService.insertTApprovalRepair(tApprovalRepair) > 0){
            //审批通过之后更新状态 审批状态（0:通过，1：不通过，2：待审批）
            if(tApprovalRepair.getApprovealStatus() == 0){
                TRepairManagement tRepairManagement =  new TRepairManagement();
                tRepairManagement.setRepairId(tApprovalRepair.getRepairId());
                tRepairManagement.setOrderStatus(1L);//订单状态（0:已申请，1：已审批，2：已送修，3：维修完成，4：撤销订单）
                tRepairManagementService.saveOrUpdate(tRepairManagement);
            }else  if(tApprovalRepair.getApprovealStatus() == 1){
                TRepairManagement tRepairManagement =  new TRepairManagement();
                tRepairManagement.setRepairId(tApprovalRepair.getRepairId());
                tRepairManagement.setOrderStatus(5L);//订单状态（0:已申请，1：已审批，2：已送修，3：已完成，4：已撤销 5：已驳回）
                tRepairManagementService.saveOrUpdate(tRepairManagement);
            }else {
                return error("审批通过之后更新状态失败");
            }

        }else {
            return error("新增申请维修审批失败");
        }
        return success();
    }

    /**
     * 修改申请维修审批
     */
    @Log(title = "申请维修审批", businessType = BusinessType.UPDATE)
    @ApiOperation("申请维修审批")
    @PutMapping
    public AjaxResult edit(@RequestBody TApprovalRepair tApprovalRepair)
    {
        return toAjax(tApprovalRepairService.updateTApprovalRepair(tApprovalRepair));
    }

    /**
     * 删除申请维修审批
     */
    @Log(title = "申请维修审批", businessType = BusinessType.DELETE)
    @ApiOperation("删除申请维修审批")
	@DeleteMapping("/{approvalIds}")
    public AjaxResult remove(@PathVariable Long[] approvalIds)
    {
        return toAjax(tApprovalRepairService.deleteTApprovalRepairByApprovalIds(approvalIds));
    }

    @ApiOperation("查询维修审批单详情")
    @GetMapping("/getApprovalRepairId")
    public AjaxResult getApprovalRepairId(@RequestParam Long repairId) {
        TApprovalRepair tApprovalRepair = new TApprovalRepair();
        tApprovalRepair.setRepairId(repairId);
        List<TApprovalRepair> list = tApprovalRepairService.selectTApprovalRepairList(tApprovalRepair);
        return success(list);
    }

    @ApiOperation("查询已审批维修审批单")
    @GetMapping("/getApprovalRepairList")
    public TableDataInfo getApprovalRepairList(@RequestParam Long approvealStatus,@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        startPage();
        LoginUser loginUser = getLoginUser();
        TApprovalRepair approvalRepair  = new TApprovalRepair();
        List<TRepairManagement> list = new ArrayList<>();
        //审批状态（0:通过，1：不通过，2：待审批）
        if(approvealStatus == 0 || approvealStatus == 1 ) {
//            approvalRepair.setApprovealStatus(approvealStatus);
            if (!loginUser.getUser().isAdmin()) {
                approvalRepair.setCreateBy(loginUser.getUser().getUserName());
            }
           list = tRepairManagementService.getApprovalRepairList(approvalRepair);
            for (TRepairManagement repairManagement : list) {
                if (ObjectUtils.isNotEmpty(repairManagement.getRepairCost())) {
                    repairManagement.setRepairCostStr(repairManagement.getRepairCost().toString());
                }
                if (ObjectUtils.isNotEmpty(repairManagement.getExpectCost())) {
                    repairManagement.setExpectCostStr(repairManagement.getExpectCost().toString());
                }
                if (ObjectUtils.isNotEmpty(repairManagement.getRepairRepair())) {
                    repairManagement.setRepairRepairStr(repairManagement.getRepairRepair().toString());
                }
            }
        }else {
            TRepairManagement tRepairManagement = new TRepairManagement();
            if (!loginUser.getUser().isAdmin()) {
                tRepairManagement.setCompanyId(loginUser.getUser().getDeptId());
            }
            tRepairManagement.setOrderStatus(0L);//订单状态（0:已申请，1：已审批，2：已送修，3：已完成，4：已撤销 5：已驳回）
            list = tRepairManagementService.selectTRepairManagementList(tRepairManagement);
            for (TRepairManagement repairManagement : list ){
                if(ObjectUtils.isNotEmpty(repairManagement.getRepairCost())) {
                    repairManagement.setRepairCostStr(repairManagement.getRepairCost().toString());
                }
                if(ObjectUtils.isNotEmpty(repairManagement.getExpectCost())){
                    repairManagement.setExpectCostStr(repairManagement.getExpectCost().toString());
                }
                if(ObjectUtils.isNotEmpty(repairManagement.getRepairRepair())){
                    repairManagement.setRepairRepairStr(repairManagement.getRepairRepair().toString());
                }
            }
        }
        return getDataTable(list);
    }
}
